﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DVDFilmoteka.src;

namespace DVDFilmoteka.Forms
{
    public partial class FormTemplateList : LocalizableForm
    {
        public String SelectedTemplate { get; set; }

        public FormTemplateList()
        {
            InitializeComponent();

            SelectedTemplate = "";
        }

        private void FormTemplateList_Load(object sender, EventArgs e)
        {
            listBoxTemplates.Items.Clear();
            foreach (String item in TemplateManager.Instance.AvailableTemplateNames)
            {                
                if (!"default".Equals(item)) listBoxTemplates.Items.Add(item);
            }

            listBoxTemplates_SelectedIndexChanged(sender, e);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (listBoxTemplates.SelectedItem != null)
            {
                SelectedTemplate = listBoxTemplates.SelectedItem as String;
            }

            this.Close();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listBoxTemplates.SelectedItem == null) return;

            String name = "";
            try
            {
                name = listBoxTemplates.SelectedItem as String;
            }
            catch { return; }

            DialogResult result = MessageBox.Show(this, String.Format(Properties.Messages.messages_deletetemplate, name), Tools.AssemblyName, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                TemplateManager.Instance.DeleteTemplateByName(name);
            }

            // Prenacist list
            FormTemplateList_Load(sender, e);
        }

        private void listBoxTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            buttonDelete.Enabled = listBoxTemplates.SelectedItem != null;
            buttonSelect.Enabled = listBoxTemplates.SelectedItem != null;
        }

        public override void Localize()
        {
            this.Text = Properties.Messages.form_templatelist_text;
            buttonCancel.Text = Properties.Messages.form_templatelist_buttonCancel;
            buttonDelete.Text = Properties.Messages.form_templatelist_buttonDelete;
            buttonSelect.Text = Properties.Messages.form_templatelist_buttonSelect;
        }
    }
}
